package org.example.utils;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 *
 * @author zjj
 * @date 2019/3/27 0027
 */
public class DateUtils {

    public static String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String ORACLE_DATA_PATTERN = "yyyyMMdd HH:mm:ss";

    public static String getCurrentDate(String pattern) {
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        Date date = calendar.getTime();
        return new SimpleDateFormat(pattern).format(date);
    }

    public static String getDefaultCurrentDate() {
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        Date date = calendar.getTime();
        return new SimpleDateFormat(DEFAULT_PATTERN).format(date);
    }

    public static String formatDefaultPatternDate(Date date){
        return null == date ? null : formatDate(date, DEFAULT_PATTERN);
    }

    public static String formatDate(Date date ,String pattern){
        return null == date ? null : new SimpleDateFormat(pattern).format(date);
    }

    public static Date parseDefaultPatternDate(String strDate) {
        return StringUtils.isEmpty(strDate) ? null : new SimpleDateFormat(DEFAULT_PATTERN).parse(strDate, new ParsePosition(0));
    }

    public static Date parseDate(String strDate, String patten) {
        return StringUtils.isEmpty(strDate) ? null : new SimpleDateFormat(patten).parse(strDate, new ParsePosition(0));
    }

    public static void main(String[] arg){
        try {
            String s = "20190520208000";
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmSS");
            System.out.println(s.equals(format.format(format.parse(s))));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取今天下午17点
     * @return
     */
    public static Date getTodaySeventeen() {
        Date date = new Date();
        //date.setHours(17);
        //date.setMinutes(0);
        //date.setSeconds(0);
        //return date;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 17);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取明天上午10点
     * @return
     */
    public static Date getTomorrowTen() {
        Date date = new Date();
        //date.setDate(date.getDate()+1);
        //date.setHours(10);
        //date.setMinutes(0);
        //date.setSeconds(0);
        //return date;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 10);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getCurrentTime(int i, int minute) {
        Calendar nowTime = Calendar.getInstance();
        if (i > 0) {
            nowTime.add(minute, i);
        } else {
            nowTime.roll(minute, i);
        }
        return nowTime.getTime();
    }
}
